<?php

if (!defined('BASEPATH'))  exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */
// ------------------------------------------------------------------------

/**
 * Utility Class
 *
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @category	Utilize
 * @author		ExpressionEngine Dev Team
 * @link		http://codeigniter.com/user_guide/general/errors.html
 */
class Utility {

    var $CI;

    /**
     * Constructor
     */
    public function __construct() {
        $this->CI = & get_instance();
    }

    // --------------------------------------------------------------------

    /**
     * Check if file is existed in temp...
     *
     * Generally this function will be called using the global log_message() function
     *
     * @param	string	the error level
     * @param	string	the error message
     * @param	bool	whether the error is a native PHP error
     * @return	bool
     */
    public function is_duplicate() {
        // use raw post data
        if ((bool) $this->CI->input->get_post('uniqid')) {
            $uniqid = $this->CI->input->get_post('uniqid');
        } else {
            return false;
        }
        // delete old files over timeout.
        $this->delete_tmp("uniqid_", 60 * 60 * 1);
        $file = sprintf("%s/uniqid_%s_%s", APPPATH . "/" . $this->CI->config->item('tmp'), $this->CI->input->ip_address(), $uniqid);

        if (!(bool) file_exists($file)) {
            touch($file);
            return false;
        }

        $st = stat($file);
        if ($st[9] + 60 * 60 * 1 < time()) {
            return false;
        }
        return true;
    }

    public function delete_tmp($prefix, $timeout) {
        $dir = opendir(APPPATH . "/" . $this->CI->config->item('tmp'));
        if ($dir) {
            while (($file = readdir($dir)) !== false) {
                if (strncmp($file, $prefix, strlen($prefix)) == 0) {
                    $f = APPPATH . "/" . $this->CI->config->item('tmp') . "/" . $file;
                    $st = @stat($f);
                    if ($st[9] + $timeout < time()) {
                        unlink($f);
                    }
                }
            }
            closedir($dir);
        }
    }

    public function clear_duplicate() {
        if ((bool) $this->CI->input->post('uniqid')) {
            $uniqid = $this->CI->input->post('uniqid');
        } else {
            return false;
        }

        $file = sprintf("%s/uniqid_%s_%s", APPPATH . "/" . $this->CI->config->item('tmp'), $this->CI->input->ip_address(), $uniqid);

        if (file_exists($file)) {
            if (unlink($file) == false) {
                return true;
            }
        }
        return false;
    }

}

// END Utility Class

/* End of file Utility.php */
/* Location: ./system/libraries/Utility.php */